package com.mukefile.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mukefile.dto.DeleteFileDto;
import com.mukefile.dto.FileListDto;
import com.mukefile.dto.UserFileDto;
import com.mukefile.pojo.*;
import com.mukefile.service.UserFileService;
import com.mukefile.utils.CommonUtil;
import com.mukefile.utils.constant.Constant;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * 用户文件控制器
 * @author xinke.huang@hand-china.com
 * @version 1.0
 * @date 2021/2/10
 */
@Controller
@Scope("prototype")
public class UserFileAction extends ActionSupport implements ModelDriven<UserFileDto> {
    /**
     * 获取传输对象
     */
    private final UserFileDto userFileDto = new UserFileDto();
    @Override
    public UserFileDto getModel() {
        return userFileDto;
    }

    /**
     * 获取session
     */
    private final Map<String, Object> session = ActionContext.getContext().getSession();
    @Resource
    private UserFileService userFileService;

    /**
     * 文件列表页面
     * @return 文件列表页面
     */
    public String showFileListPage(){
        return "file_list_page";
    }

    /**
     * 新建文件夹
     */
    public void addFolder(){
        // 获取用户对象
        User user = (User) session.get(Constant.SESSION_USER);
        // 创建文件夹对象
        UserFolder folder = new UserFolder();
        folder.setUserId(user.getId());
        folder.setFolderName(this.userFileDto.getFolderName());
        folder.setParentFolderId(this.userFileDto.getParentFolderId());
        folder.setUpdateTime(new Date());
        folder.setDeleted("N");
        folder.setType("folder");
        try {
            // 执行新增方法
            userFileService.addFolder(folder);
            CommonUtil.responseJSON("ok");
        }catch (Exception e){
            try {
                CommonUtil.responseJSON("error");
            } catch (IOException e2) {
                e2.printStackTrace();
            }
            e.printStackTrace();
        }
    }

    /**
     * 重命名
     */
    public void rename(){
        Long id = this.userFileDto.getId();
        String name = this.userFileDto.getName();
        String type = this.userFileDto.getType();
        try {
            if (Objects.equals(type,Constant.FOLDER)){
                // 文件夹重命名
                UserFolder folder = new UserFolder();
                folder.setId(id);
                folder.setFolderName(name);
                folder.setUpdateTime(new Date());
                userFileService.renameFolder(folder);
            } else {
                // 文件重命名
                UserFile file = new UserFile();
                file.setId(id);
                file.setFilename(name);
                file.setUpdateTime(new Date());
                userFileService.renameFile(file);
            }
            CommonUtil.responseJSON("ok");
        }catch (Exception e){
            try {
                CommonUtil.responseJSON("error");
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }

    }
    /**
     * 分页查询所有文件数据
     */
    public void queryFileList(){
        // 获取用户对象
        User user = (User) session.get(Constant.SESSION_USER);

        try {
            if (this.userFileDto.getParentFolderId() == null){
                this.userFileDto.setParentFolderId(-1L);
            }
            ResponseData<FileListDto> responseData = userFileService.getAllFileAndFolder(
                    user.getId(),this.userFileDto.getParentFolderId(),
                    this.userFileDto.getPage(), this.userFileDto.getLimit());
            CommonUtil.responseJSON(responseData);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据id获取文件夹
     */
    public void getFolderById(){
        // 根据id主键查询文件夹对象
        try {
            UserFolder folder = this.userFileService.getFolderById(this.userFileDto.getId());
            CommonUtil.responseJSON(folder);
        } catch (IOException e) {
            try {
                CommonUtil.responseJSON("error");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            e.printStackTrace();
        }
    }

    /**
     * 批量删除文件
     */
    public void deleteFile(){

        try {
            String deleteFiles = this.userFileDto.getDeleteFiles();
            JSONArray jsonArray = JSONArray.parseArray(deleteFiles);
            List<DeleteFileDto> list = new ArrayList<>(jsonArray.size());
            for (int i=0;i<jsonArray.size();i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                DeleteFileDto deleteFileDto = new DeleteFileDto();
                deleteFileDto.setId(jsonObject.getLong("id"));
                deleteFileDto.setType(jsonObject.getString("type"));
                list.add(deleteFileDto);
            }
            this.userFileService.deleteFiles(list);
            CommonUtil.responseJSON("ok");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
